home *** CD-ROM | disk | FTP | other *** search
- /*=============================================================================*/
- inquire(has(X,Y,W,Z),T) :-
- print('Does the ',X,' have ',Y,'?'),
- !,nl,
- get_answer(A),
- respond(A,has(X,Y,W,Z),T),!.
- inquire(gives(X,Y,W,Z),T) :-
- print('Does the ',X,' give ',Y,'?'),
- !,nl,
- get_answer(A),
- respond(A,gives(X,Y,W,Z),T),!.
- inquire(eats(X,Y,W,Z),T) :-
- print('Does the ',X,' eat ',Y,'?'),
- !,nl,
- get_answer(A),
- respond(A,eats(X,Y,W,Z),T),!.
- inquire(flies(X,Y,W,Z),T) :-
- print('Does the ',X,' fly ',Y,'?'),
- !,nl,
- get_answer(A),
- respond(A,flies(X,Y,W,Z),T),!.
- inquire(swims(X,_,W,Z),T) :-
- print('Does the ',X,' swim?' ),
- !,nl,
- get_answer(A),
- respond(A,swims(X,' ',W,Z),T),!.
- inquire(size(X,Y,W,Z),T) :-
- print('Is the ',X,' ',Y,'?'),
- !,nl,
- get_answer(A),
- respond(A,size(X,Y,W,Z),T),!.
- inquire(breathing(X,Y,W,Z),T) :-
- print('Does the ',X,' breath ',Y,'?'),
- !,nl,
- get_answer(A),
- respond(A,breathing(X,Y,W,Z),T),!.
- inquire(vertebrate(X,_,W,Z),T) :-
- print('Is the ',X,' a vertebrate?'),
- !,nl,
- get_answer(A),
- respond(A,vertebrate(X,' ',W,Z),T),!.
-
- respond(yes,X(A,B,[H|L],[D]),T) :-
- concat(D,[' \nFACT: ',A,' ',X,' ',B]),
- asserta(X(A,B,_,[D])).
-
- respond(why,X,[]) :-
- print('You should know!'),
- nl,
- inquire(X,[]).
-
- respond(why,X,[H|T]) :-
- print(H),
- nl,!,
- inquire(X,T).
-
- has(A,B,L,M) :-
- inquire(has(A,B,L,M),L),!.
-
- gives(A,B,L,M) :-
- inquire(gives(A,B,L,M),L),!.
- eats(A,B,L,M) :-
- inquire(eats(A,B,L,M),L),!.
- flies(A,B,L,M) :-
- inquire(flies(A,B,L,M),L),!.
- swims(A,_,L,M) :-
- inquire(swims(A,_,L,M),L),!.
- size(A,B,L,M) :-
- inquire(size(A,B,L,M),L),!.
- breathing(A,B,L,M) :-
- inquire(breathing(A,B,L,M),L),!.
- vertebrate(A,B,L,M) :-
- inquire(vertebrate(A,B,L,M),L),!.
- identified_as_a(A,mammal,M,L) :-
- concat(Z,['\nRULE 1: ',A,' is a mammal if it has hair.']),
- append(M,[Z],X),
- has(A,hair,X,W),!,
- append([Z],W,L),
- asserta(identified_as_a(A,mammal,M,L)).
- identified_as_a(A,mammal,M,L) :-
- concat(Z,['\nRULE 2: ',A,' is a mammal if it gives milk.']),
- append(M,[Z],X),
- gives(A,milk,X,W),!,
- append([Z],W,L),
- asserta(identified_as_a(A,mammal,M,L)).
-
- identified_as_a(A,bird,M,L) :-
- concat(Z,['\nRULE 3: ',A,' is a bird if it has feathers.']),
- append(M,[Z],X),
- has(A,feathers,X,W),!,
- append([Z],W,L),
- asserta( identified_as_a(A,bird,M,L)).
- identified_as_a(A,bird,M,L) :-
- concat(Z,['\nRULE 4: ',A,' is a bird if it flies.']),
- append(M,[Z],X),
- flies(A,well,X,W),!,
- append([Z],W,L),
- asserta(identified_as_a(A,bird,M,L)).
-
- identified_as_a(A,carnivore,M,L) :-
- concat(Z,['\nRULE 5: ',A,' is a carnivore if it eats meat.']),
- append(M,[Z],X),
- eats(A,meat,X,W),!,
- append([Z],W,L),
- asserta(identified_as_a(A,carnivore,M,L)).
-
- identified_as_a(A,reptile,M,L) :-
- concat(Z,['\nRULE 6: ',A,' is a reptile if it is an air breathing vertebrate.']),
- append(M,[Z],X),
- breathing(A,air,X,W),
- vertebrate(A,_,X,N),
- append([Z],W,R),
- append(R,N,L),
- asserta(identified_as_a(A,reptile,M,L)).
-
- identified_as_a(A,tiger) :-
- concat(Z,['\nRULE 7: ',A,' is a tiger if it is a mammal,','\n',
- 'and a carnivore, and has black stripes']),
- identified_as_a(A,mammal,[Z],M),
- identified_as_a(A,carnivore,[Z],N),
- has(A,'black stripes',[Z],E),
- print(A,' is a tiger.'),nl,!,
- append(M,N,W),
- append(W,E,G),
- asserta(identified_as_a(A,tiger,[Z],[Z|G])).
-
- identified_as_a(A,ostrich) :-
- concat(Z,['\nRULE 8: ',A,' is an ostrich if it is a bird','\n',
- 'has a long neck and long legs.']),
- identified_as_a(A,bird,[Z],M),
- has(A,'a long neck',[Z],C),
- has(A,'long legs',[Z],E),
- print(A,' is an ostrich.'),
- nl,!,
- append(M,C,F),
- append(F,E,G),
- asserta(identified_as_a(A,ostrich,[Z],[Z|G])).
-
- identified_as_a(A,whale) :-
- concat(Z,['\nRULE 9: ',A,' is a whale if it is a mammal','\n',
- 'has a gray color, swims and is huge.']),
- identified_as_a(A,mammal,[Z],M),
- has(A,'a gray color',[Z],N),
- swims(A,_,[Z],C),
- size(A,huge,[Z],D),
- print(A,' is a whale.'),
- nl,!,
- append(M,N,W),
- append(C,D,F),
- append(W,F,G),
- asserta(identified_as_a(A,whale,[Z],[Z|G])).
- identified_as_a(A,robin) :-
- concat(Z,['\nRULE 10: ',A,' is a robin if it is a bird and has a red breast.']),
- identified_as_a(A,bird,[Z],M),
- has(A,'a red breast',[Z],N),
- print(A,' is a robin.'),
- nl,!,
- append(M,N,G),
- asserta(identified_as_a(A,robin,[Z],[Z|G])).
-
- identified_as_a(A,rattlesnake) :-
- concat(Z,['\nRULE 11: ',A,' is a rattlesnake if it is a reptile and has rattles.']),
- identified_as_a(A,reptile,[Z],M),
- has(A,'rattles',[Z],N),
- print(A,' is a rattlesnake.'),
- nl,!,
- append(M,N,G),
- asserta(identified_as_a(A,rattlesnake,[Z],[Z|G])).
-
- how(X(A,B)) :-
- X(A,B,_,[H|T]),!,
- print(H),
- !,
- how(T),!.
- how([]) :-
- print('\nAND THAT IS ALL THERE IS TO IT.'),
- !.
-
- how([H|T]) :-
- print(H),
- how(T),!.
-
-
-
- help :- forgetuser,
- print('Let me guess which of the following animals you have in mind:'),
- nl,
- print('tiger,ostrich,robin, whale, or rattlesnake.'),
- nl,nl,
- knowledge_domain(X),!,
- deduce(X),!.
-
- forgetuser :-
- print('\nDo you want me to forget anything you have already taught me?'),
- get_answer( A ),
- wipemem( A ).
-
- wipemem( A ) :- A = yes, forget( user ).
- wipemem( X ).
-
- deduce([]) :-
- print( 'Sorry, I don\'t know enough to guess this one.' ), !.
-
- deduce([H|T]) :-
- identified_as_a(animal,H),!,
- nl,
- print('Do you want to know how I concluded this? '),!,
- get_answer( A ),
- A = yes,
- nl,
- how( identified_as_a(Animal,H)),!.
-
- deduce([H|T]) :-
- !,
- deduce(T).
-
- knowledge_domain([robin,rattlesnake,ostrich,whale,tiger]).
-
-
- append([],L,L).
- append([Z|L1],L2,[Z|L3]) :- append( L1,L2,L3 ).
-
-
- get_answer( A ) :-
- ratom( X ), name( X, String ),
- valid_resp( String, A ), !.
-
- valid_resp( [H|T], A ) :- type_ans( H, A ).
-
- type_ans( X, A ) :- ([X] = "y"; [X] = "Y"), A = yes.
- type_ans( X, A ) :- ([X] = "n"; [X] = "N"), A = no.
- type_ans( X, A ) :- ([X] = "w"; [X] = "W"), A = why.
-
- ?-print('\nYou can start animal by typing "help.<CR>"\n' ).
-
- valid_resp( [], A ) :-
- print('\nPlease try to give me a yes or no answer.'),
- get_answer( A ), !.
-
- valid_resp( [H|T], A ) :- valid_resp( T, A ).
-
- /*=============================================================================*/